package guoanmaker.operator.business.service;

import guoanmaker.operator.business.model.ProvideServerClass;
import guoanmaker.operator.business.repository.ProvideServerClassRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Light on 2017/6/22.
 */
@Service
public class ProvideServerClassService {
    private final ProvideServerClassRepository provideServerClassRepository;

    @Autowired
    public ProvideServerClassService(ProvideServerClassRepository provideServerClassRepository) {
        this.provideServerClassRepository = provideServerClassRepository;
    }

    public ProvideServerClass saveServerClass(ProvideServerClass provideServerClass){
        return provideServerClassRepository.save(provideServerClass);
    }

    /** 
    * @Title: findAll
    * @Description: 查询所有服务分类
    * @param    
    * @return 结果集合   
    * @throws 
    */
    public List<ProvideServerClass> getAllServerClass(){
        List<ProvideServerClass> provideServerClassList = provideServerClassRepository.findAll();
        ArrayList<ProvideServerClass> resultList = new ArrayList<ProvideServerClass>();
        if (provideServerClassList != null || !provideServerClassList.isEmpty()) {
            resultList.addAll(provideServerClassList);
        }
        return sortProvideServerClass(resultList);
    }
    /** 
    * @Title: getProviderClassByPage
    * @Description: 分页查询服务分类
    * @param： Integer page 当前页
     * @param: Integer size 每页条数   
    * @return Page<ProvideServerClass>    
    * @throws 
    */
    public Page<ProvideServerClass> getServerClassByPage(Integer page, Integer size){
        Sort sort = new Sort(Sort.Direction.ASC, "serverLevel");
        Pageable pageable = new PageRequest(page, size, sort);
        Page<ProvideServerClass> provideServerClassList = provideServerClassRepository.findAll(pageable);
        return provideServerClassList;
    }

    /** 
    * @Title: getServerClassById 
    * @Description: 根据ID查找服务分类
    * @param id   
    * @return    
    * @throws 
    */

    public ProvideServerClass getServerClassById(String id) {
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        return provideServerClassRepository.findOne(id);
    }

    /** 
     * @Title: getServerClassByServerLevel
     * @Description: 根据服务分类级别查询
     * @param：id   
     * @return    
     * @throws 
     */
    public Page<ProvideServerClass> getServerClassByServerLevel(Integer page, Integer size, Integer serverLevel) {
        Pageable pageable = new PageRequest(page, size);
        Page<ProvideServerClass> provideServerClassesList = provideServerClassRepository.findAllByServerLevel(pageable, serverLevel);
        return provideServerClassesList;
    }

    /** 
    * @Title: getServerClassByParentServerId 
    * @Description: 根据父项ID查询
    * @param: parentServerId  父项ID 
    * @return   
    * @throws 
    */
    public ArrayList<ProvideServerClass> getServerClassByParentServerId(String parentServerId) {
        if (StringUtils.isEmpty(parentServerId))
            return null;
        ArrayList<ProvideServerClass> provideServerClassArrayList = provideServerClassRepository.findAllByParentServerId(parentServerId);
        return provideServerClassArrayList;
    }

    /** 
     * @Title: deleteServerClass
     * @Description: 根据ID删除服务分类
     * @param：id   
     * @return    
     * @throws 
     */
    public void deleteServerClass(String id){
        provideServerClassRepository.delete(id);
    }

    /** 
    * @Title: sortProvideServerClass
    * @Description: ProvideServerClass集合排序
    * @param： ArrayList<ProvideServerClass>   
    * @return ArrayList<ProvideServerClass>    
    * @throws 
    */
    private static ArrayList<ProvideServerClass> sortProvideServerClass(ArrayList<ProvideServerClass> provideServerClassArrayList){
            provideServerClassArrayList.sort(new Comparator<ProvideServerClass>() {
                @Override
                public int compare(ProvideServerClass o1, ProvideServerClass o2) {
                    if (o1.getServerLevel() != o2.getServerLevel())
                        return o1.getServerLevel() - o2.getServerLevel();
                    else
                        return o1.getId().compareTo(o2.getId());
                }
            });
            return provideServerClassArrayList;
    }

}
